import java.awt.Color;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JButton;


public class BoutonsCouleurs extends JPanel {
	private static final long serialVersionUID = 1L;

	private Color couleurCourante;
	private PropertyChangeSupport changes;
	
	public BoutonsCouleurs()
	{
		changes = new PropertyChangeSupport(this);
		//setLayout(new GridLayout(8,1));
		
		GridBagLayout g = new GridBagLayout();
		setLayout(g);
		GridBagConstraints c = new GridBagConstraints () ;
		c.fill = GridBagConstraints.BOTH ; 
		c.weightx = 0 ;
		c.gridwidth = GridBagConstraints.REMAINDER ;
		JButton temp = new JButton(new ImageIcon("images/iconRouge.png"));
		temp.addActionListener(new ButtonListener(Color.red));
		g.setConstraints(temp,c);
		add(temp);
		temp = new JButton(new ImageIcon("images/iconBleu.png"));
		temp.addActionListener(new ButtonListener(Color.blue));
		g.setConstraints(temp,c);
		add(temp);
		temp = new JButton(new ImageIcon("images/iconVert.png"));
		temp.addActionListener(new ButtonListener(Color.green));
		g.setConstraints(temp,c);
		add(temp);
		temp = new JButton(new ImageIcon("images/iconJaune.png"));
		temp.addActionListener(new ButtonListener(Color.yellow));
		g.setConstraints(temp,c);
		add(temp);
		temp = new JButton(new ImageIcon("images/iconNoir.png"));
		temp.addActionListener(new ButtonListener(Color.black));
		g.setConstraints(temp,c);
		add(temp);
		temp = new JButton(new ImageIcon("images/iconBlanc.png"));
		temp.addActionListener(new ButtonListener(Color.white));
		g.setConstraints(temp,c);
		add(temp);
		temp = new JButton(new ImageIcon("images/iconOrange.png"));
		temp.addActionListener(new ButtonListener(Color.orange));
		g.setConstraints(temp,c);
		add(temp);
		temp = new JButton(new ImageIcon("images/iconViolet.png"));
		temp.addActionListener(new ButtonListener(Color.getHSBColor((float) 0.8, 1, (float) 0.9)));
		g.setConstraints(temp,c);
		add(temp);
	}
	
	/* Accessors */
	public void setCurrentButton(Color c)
	{
		Color oldColor = couleurCourante;
		couleurCourante = c;
		changes.firePropertyChange("Couleur", oldColor, couleurCourante);
	}
	
	public void addPropertyChangeListener(PropertyChangeListener l)
	{
		changes.addPropertyChangeListener(l);
	}
	
	public void removePropertyChangeListener(PropertyChangeListener l)
	{
		changes.removePropertyChangeListener(l);
	}
	
	/* Action Listeners Methods */
	class ButtonListener implements ActionListener {

		Color couleur;
		
		public ButtonListener(Color c)
		{
			couleur = c;
		}
		
		@Override
		public void actionPerformed(ActionEvent e) {
			BoutonsCouleurs.this.setCurrentButton(couleur);
		}
		
	}
}
